class_name BulletResidualEffect
extends Node2D


## 该节点作为bullet节点的子节点使用


@export var target: Node2D
@export var display_duration: float = 1.0
@export var fade_duration: float = 0.25

var _running: bool
var _follow_node: Node2D
var _pos_offset: Vector2
var _original_a: float

signal finished


func _process(_delta: float) -> void:
    if not _running or not _follow_node: return

    get_parent().global_position = _follow_node.global_position - _pos_offset


## follow_node: 残留的位置随follow_node位置更新
func display(follow_node: Node2D = null) -> void:
    if follow_node:
        _pos_offset = follow_node.global_position - get_parent().global_position
    _follow_node = follow_node
    _running = true
    
    await get_tree().create_timer(display_duration).timeout
    var tween := get_tree().create_tween()
    _original_a = target.modulate.a
    tween.tween_property(target, "modulate:a", 0, fade_duration)
    tween.finished.connect(
        func() -> void:
            target.modulate.a = _original_a
            _running = false
            finished.emit()
    )
    tween.play()
